import logging
from functools import partial
from PyQt5.QtWidgets import (QPushButton, QTableWidgetItem, QHeaderView, 
                             QFrame, QHBoxLayout, QMessageBox, QDialog)

from ..vacwx import IVacWidget

class MenuBar(IVacWidget):
    """ 提供类似 Microsoft-Word 程序的可下拉菜单栏 """
    def __init__(self, parent=None, uifile=None):  # uifile="frw/ui/menu_bar.ui"
        # self.parent = parent
        super().__init__(parent, uifile)

        """ # Here is a TEST!!
        list_btns_name_info = [
            ("menu_0", ["sub_0", "sub_1", "sub_2"]),
            ("menu_1", []),
            ("menu_2", ["name_0", "nuam_1"])
        ]
        self.menu_bars_init(list_btns_name_info)
        """

    def menu_bars_init(self, list_btns_name_info):
        """ info的结构为： [("btn_name": list_submenu_btns_name), ... ] """
        for index, tuple_info in enumerate(list_btns_name_info):
            btn_name, list_submenu_btns_name = tuple_info
            # 创建按钮
            btn_wx = QPushButton(btn_name)  # , self.menu_btn_frame
            btn_wx.clicked.connect(partial(self.on_menu_btn_clicked,
                    index, False if list_submenu_btns_name else True))
            self.menus_layout.addWidget(btn_wx)

            # 根据当前的menu_btn，初始化sub_title
            self.submenu_bars_init(list_submenu_btns_name, index)

    # @virtual
    def on_menu_btn_clicked(self, menu_btn_index, toHide=False):
        if toHide:
            self.submenubar_frame.hide()
            return
        # else:
        self.submenubar_frame.setCurrentIndex(menu_btn_index)
        self.submenubar_frame.show()

    def submenu_bars_init(self, list_btns_id, stackwx_index=None):
        """ name的结构为 ["btn_name", ... ] """
        panel_frame = QFrame()
        panel_layout = QHBoxLayout()
        panel_frame.setLayout(panel_layout)

        if stackwx_index:
            self.submenubar_frame.insertWidget(stackwx_index, panel_frame)
        else:
            self.submenubar_frame.addWidget(panel_frame)

        for sub_btn_id in list_btns_id:
            submenu_btn = QPushButton(sub_btn_id)
            submenu_btn.clicked.connect(
                    partial(self.on_submenu_btn_clicked, sub_btn_id))
            # 默认将按钮添加，并隐藏
            panel_layout.addWidget(submenu_btn)

    # @abstract
    def on_submenu_btn_clicked(self, btn_info):pass


if __name__ == "__main__":
    # python3 -m gui.wx.menu_bar
    from frw.entry import qt5_program_run
    qt5_program_run(MenuBar, 20)